const app = getApp();
var toUrl = require('../../utils/toUrl.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    position: null
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPosition: "",
    configimg: [],
    banners: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false, // loading
    swiperCurrent: 0
  },
  ready: function () {
    var imgPosition = this.data.position;
    this.setData({
      imgPosition: imgPosition
    })
    this.GetImgsList();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    GetImgsList: function () {
      var that = this;
      wx.request({
        url: app.globalData.host + '/api/Store/GetWidgetList?storeId=' + app.globalData.storeId + '&code=index_pic_area&position=' + that.data.imgPosition,
        success: function (res) {
          if (res.data.Code == 0) {
            var ConfigOne = res.data.Data.Config
            for (var i = 0; i < ConfigOne.length; i++) {
              var CustomerFeatures = ConfigOne[i].CustomerFeatures
              for (var j = 0; j < CustomerFeatures.length; j++) {
                CustomerFeatures[j].ShowImg = app.globalData.image_logo_banner + CustomerFeatures[j].ShowImg
              }
            }
            var heightArr = [];
            var f = 0;
            for (let k = 0; k < ConfigOne.length; k++) {//获取图片宽高
              let customerFeatures = ConfigOne[k].CustomerFeatures;
              if (ConfigOne[k].DisplayType == 16) {
                for (let h = 0; h < customerFeatures.length; h++) {
                  wx.getImageInfo({
                    src: customerFeatures[h].ShowImg,
                    success: function (res) {
                      customerFeatures[h].width = res.width;
                      customerFeatures[h].height = res.height;
                    },

                    complete: function (res) {
                      f++
                      if (f == customerFeatures.length) {
                        console.log('执行完毕');
                        for (let g = 0; g < customerFeatures.length; g++) {
                          //获取图片真实宽度  
                          var imgwidth = customerFeatures[g].width,
                            imgheight = customerFeatures[g].height,
                            //宽高比  
                            ratio = imgwidth / imgheight;
                          //计算的高度值  
                          var viewHeight = 750 / ratio;
                          var imgheight = viewHeight
                          heightArr.push(imgheight)
                          customerFeatures[g].imgheight = imgheight;
                        }
                        for (let t = 0; t < ConfigOne.length; t++) {
                          var maxN = that.checkMax(heightArr)
                          ConfigOne[t].maxHeight = maxN;
                        }
                        that.setData({
                          configimg: ConfigOne
                        })
                      } else {
                        console.log("继续");
                      }
                    }
                  })
                }
              }
            }
            that.setData({
              configimg: ConfigOne
            })
          }
        }
      })
    },

    //判断最大值
    checkMax: function (heightArr) {
      var maxN = Math.max.apply(null, heightArr);
      return maxN;
    },

    swiperchange: function (e) {
      this.setData({
        swiperCurrent: e.detail.current
      })
    },
    //页面跳传
    tapBanner: function (options) {
      toUrl.tapBanner(options)
    }
  }
})
